/*
 * simp.java
 *
 * Created on 5 Декабрь 2010 г., 13:39
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package protocol.simp;

import DrawControls.icons.*;

import chatty.*;
import chatty.comm.*;
import chatty.Options;
import chatty.search.*;

import protocol.*;

/**
 *
 * @author Rushan Ahmetshin
 */
public class Simp extends Protocol {
    private static ImageList statusIcons;
    private static int[] statusIconIndex =  {1, 0, 4, 3, 5, 6, -1, -1, -1, -1, -1, 2, -1};
	
	private static int[] statusIconIndexIcq =  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, -1};
	//private static int[] statusIconIndexMrim =  {0, 1, 2, 3, 4, 5, -1, -1, -1, -1, -1, 2, -1};
	
    private SimpConnection connection;
    // #sijapp cond.if modules_XSTATUSES is "true" #
    private XStatus xStatus = new XStatus((byte)-1, null);
    // #sijapp cond.end #

	private int type = 0;
	
    public Simp( byte type ) {
	
		switch (type) {

            case Profile.PROTOCOL_ICQ:
				statusIcons = ImageList.createImageList("/icq-status.png");
				info = new StatusInfo(statusIcons, statusIconIndexIcq);
				return;
				
            case Profile.PROTOCOL_MRIM:
				statusIcons = ImageList.createImageList("/mrim-status.png");
				//info = new StatusInfo(statusIcons, statusIconIndexMrim);
				//return;
				break;
				
            case Profile.PROTOCOL_JABBER:
				statusIcons = ImageList.createImageList("/jabber-status.png");
				break;
            case Profile.PROTOCOL_GTALK:
				statusIcons = ImageList.createImageList("/gtalk-status.png");
				break;
            case Profile.PROTOCOL_FACEBOOK:
				statusIcons = ImageList.createImageList("/facebook-status.png");
				break;
            case Profile.PROTOCOL_LJ:
				statusIcons = ImageList.createImageList("/livejournal-status.png");
				break;
            case Profile.PROTOCOL_YANDEX:
				statusIcons = ImageList.createImageList("/ya-status.png");
				break;
            case Profile.PROTOCOL_VK:
				statusIcons = ImageList.createImageList("/vk-status.png");
				break;
            case Profile.PROTOCOL_QIP:	
				statusIcons = ImageList.createImageList("/qip-status.png");
                break;
        }
		
		this.type = (int)type;
		info = new StatusInfo(statusIcons, statusIconIndex);
    }
	
	public int getType() {
		return type;
	}
	static Icon getPhoneContactIcon() {
        int phoneContactIndex = statusIconIndex[Status.I_STATUS_OFFLINE];
        if (6 < statusIcons.size()) {
            phoneContactIndex = statusIcons.size();
        }
        return statusIcons.iconAt(phoneContactIndex);
    }
    public boolean isEmpty() {
        return super.isEmpty();
    }

    protected void s_setPrivateStatus() {
    }

    protected void s_renameContact(Contact contact, String name) {
        contact.setName(name);
        connection.renameContact(contact, name);
    }
    protected void s_renameGroup(Group group, String name) {
        group.setName(name);
        connection.renameGroup(group, name);
    }


    protected void s_removeContact(Contact c) {
        connection.removeContact( c );
    }
    protected void s_removeGroup(Group group) {
        connection.removeGroup( group );
    }

    protected void s_moveContact(Contact contact, Group to) {
        connection.moveContact(contact, to );
    }
	
    protected void s_addContact(Contact contact) {
        connection.addContact(contact);
    }
    protected void s_addGroup(Group group) {
        connection.addGroup(group);
    }
	
    public boolean isConnected() {
        return null != connection;
    }
    SimpConnection getConnection() {
        return connection;
    }

    protected void startConnection(boolean reconnect) {
        SimpConnection c = new SimpConnection(this, reconnect);
        connection = c;
        new Thread(c).start();
    }

    protected void closeConnection() {
        SimpConnection c = connection;
        connection = null;
        if (null != c) {
            c.disconnect();
        }
    }
	
	private int getNextGroupId() {
        while (true) {
            int id = Util.nextRandInt() % 0x1000;
            for (int i = groups.size() - 1; i >= 0; --i) {
                Group group = (Group)groups.elementAt(i);
                if (group.getId() == id) {
                    id = -1;
                    break;
                }
            }
            if (0 <= id) {
                return id;
            }
        }
    }
	
    public Group createGroup(String name) {
        Group group = new Group(this, name);
		group.setGroupId(getNextGroupId());
        return group;
    }

    public Group getOrCreateGroup(String groupName) {
        if (StringConvertor.isEmpty(groupName)) {
            return null;
        }
        Group group = getGroup(groupName);
        if (null == group) {
            group = createGroup(groupName);
            addGroup(group);
        }
        return group;
    }
	
    protected Contact createContact(String uin, String name) {
        return new SimpContact(this, uin, name);
    }
	
	protected void s_sendTypingNotify(Contact to, boolean isTyping) {
		connection.sendTypingNotify(to, isTyping);
    }
	
    protected void s_searchUsers(Search cont) {
        if (null != cont.getSearchParam(Search.UIN)) {
            UserInfo info = new UserInfo(this);
            info.uin = cont.getSearchParam(Search.UIN);
            cont.addResult(info);
            cont.finished();
            return;
        }
        connection.searchUsers(cont);
    }

    protected void s_updateOnlineStatus() {
        connection.sendStatus();
		connection.sendXStatus();
    }

	public void getAvatar(UserInfo info) {
		connection.getAvatar(info);
    }
    public void saveUserInfo(UserInfo info) {
		connection.saveUserInfo(info);
    }

    private static final byte[] statuses = {
		Status.I_STATUS_ONLINE,
        Status.I_STATUS_FFC,
        Status.I_STATUS_AWAY,
        Status.I_STATUS_DND,
        Status.I_STATUS_NA,
        Status.I_STATUS_INVISIBLE
	};
	private static final byte[] icq_statuses = {
		Status.I_STATUS_ONLINE,
        Status.I_STATUS_FFC,
        Status.I_STATUS_AWAY,
        Status.I_STATUS_DND,
        Status.I_STATUS_NA,
		Status.I_STATUS_EVIL,
		Status.I_STATUS_DEPRESSION,
		Status.I_STATUS_HOME,
		Status.I_STATUS_WORK,
		Status.I_STATUS_LUNCH,
        Status.I_STATUS_INVISIBLE
	};
	private static final byte[] mrim_statuses = {
		Status.I_STATUS_ONLINE,
        Status.I_STATUS_FFC,
        Status.I_STATUS_AWAY,
        Status.I_STATUS_DND,
        Status.I_STATUS_NA,
        Status.I_STATUS_INVISIBLE
	};
	private static final byte[] vk_statuses = {
		Status.I_STATUS_ONLINE
	};
	
    public byte[] getStatusList() {
		if( getProfile().protocolType == Profile.PROTOCOL_ICQ )
			return icq_statuses;
		else if( getProfile().protocolType == Profile.PROTOCOL_VK )
			return vk_statuses;
			
        return statuses;
    }
	public void getClientInfo(String userId) {
		SimpConnection c = connection;
        if (null != c) {
            c.getClientInfo(userId);
        }
    }
    void requestAuth(String userId) {
        SimpConnection c = connection;
        if (null != c) {
            c.sendAuthRequest(userId);
        }
    }

    void grandAuth(String userId) {
        SimpConnection c = connection;
        if (null != c) {
            c.sendAuthReply(userId, true);
        }
    }

    void denyAuth(String userId) {
        SimpConnection c = connection;
        if (null != c) {
            c.sendAuthReply(userId, false);
        }
    }
	
	void removeMe(String userId) {
	    SimpConnection c = connection;
        if (null != c) {
			connection.removeMe(userId);
		}
    }
	
	public void sendSms(String phone, String text) {
		//SimpConnection c = connection;
		//c.sendMessage( phone, text );
	}
    // #sijapp cond.if modules_XSTATUSES is "true" #
    public void setXStatus(int statusIndex, String title) {
        getProfile().xstatusIndex = (byte)statusIndex;
        getProfile().xstatusTitle = title;
        getProfile().xstatusDescription = "";
        Options.saveAccount(getProfile());
        if (null != connection) {
            connection.sendXStatus();
        }
    }
    public XStatus getXStatus() {
        xStatus.setStatusIndex(getProfile().xstatusIndex);
		xStatus.setMessage(getProfile().xstatusTitle);
        return xStatus;
    }
    // #sijapp cond.end #
}